<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>刮刮卡抽奖系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            min-height: 100vh;
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            padding: 20px;
            color: #fff;
        }
        
        .container {
            width: 100%;
            max-width: 800px;
            margin: 0 auto;
        }
        
        header {
            text-align: center;
            margin-bottom: 30px;
            padding: 20px;
        }
        
        h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
        }
        
        .subtitle {
            font-size: 1.2rem;
            opacity: 0.9;
        }
        
        .main-content {
            display: flex;
            flex-wrap: wrap;
            gap: 30px;
            margin-bottom: 30px;
        }
        
        .card-area {
            flex: 1;
            min-width: 300px;
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            padding: 20px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
        }
        
        .card-container {
            position: relative;
            width: 100%;
            height: 300px;
            margin: 0 auto 20px;
            border-radius: 15px;
            overflow: hidden;
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
        }
        
        .prize-display {
            position: absolute;
            width: 100%;
            height: 100%;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            background: linear-gradient(45deg, #ff9a9e 0%, #fad0c4 100%);
            z-index: 1;
        }
        
        .prize-text {
            font-size: 2.5rem;
            font-weight: bold;
            color: #333;
            text-align: center;
            margin-bottom: 10px;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }
        
        .prize-subtext {
            font-size: 1.2rem;
            color: #555;
            text-align: center;
        }
        
        .scratch-layer {
            position: absolute;
            width: 100%;
            height: 100%;
            background: linear-gradient(45deg, #4facfe 0%, #00f2fe 100%);
            cursor: grabbing;
            z-index: 2;
        }
        
        .controls {
            display: flex;
            justify-content: center;
            gap: 15px;
        }
        
        button {
            padding: 12px 25px;
            font-size: 1.1rem;
            border: none;
            border-radius: 50px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: bold;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
        }
        
        #resetBtn {
            background: #ff6b6b;
            color: white;
        }
        
        #resetBtn:hover {
            background: #ff5252;
            transform: translateY(-3px);
        }
        
        #newCardBtn {
            background: #4cd137;
            color: white;
        }
        
        #newCardBtn:hover {
            background: #44bd32;
            transform: translateY(-3px);
        }
        
        .records-area {
            flex: 1;
            min-width: 300px;
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            padding: 20px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
        }
        
        .section-title {
            font-size: 1.5rem;
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 2px solid rgba(255, 255, 255, 0.3);
        }
        
        #recordsList {
            max-height: 300px;
            overflow-y: auto;
            padding-right: 10px;
        }
        
        .record-item {
            background: rgba(255, 255, 255, 0.15);
            border-radius: 10px;
            padding: 12px 15px;
            margin-bottom: 10px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            animation: fadeIn 0.5s ease;
        }
        
        .record-prize {
            font-weight: bold;
            font-size: 1.1rem;
        }
        
        .record-time {
            font-size: 0.9rem;
            opacity: 0.8;
        }
        
        .empty-records {
            text-align: center;
            padding: 20px;
            opacity: 0.7;
        }
        
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }
        
        @keyframes pulse {
            0% { transform: scale(1); }
            50% { transform: scale(1.05); }
            100% { transform: scale(1); }
        }
        
        .pulse {
            animation: pulse 0.5s ease;
        }
        
        /* 响应式设计 */
        @media (max-width: 768px) {
            .main-content {
                flex-direction: column;
            }
            
            .card-container {
                height: 250px;
            }
            
            h1 {
                font-size: 2rem;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>刮刮卡抽奖系统</h1>
            <p class="subtitle">刮开涂层查看奖品，试试你的手气！</p>
        </header>
        
        <div class="main-content">
            <div class="card-area">
                <div class="card-container">
                    <div class="prize-display">
                        <div class="prize-text">恭喜您获得</div>
                        <div class="prize-subtext">刮开涂层查看奖品</div>
                    </div>
                    <canvas class="scratch-layer" id="scratchCanvas"></canvas>
                </div>
                
                <div class="controls">
                    <button id="resetBtn">重置刮卡</button>
                    <button id="newCardBtn">新刮刮卡</button>
                </div>
            </div>
            
            <div class="records-area">
                <h2 class="section-title">抽奖记录</h2>
                <div id="recordsList">
                    <div class="empty-records">暂无抽奖记录</div>
                </div>
            </div>
        </div>
    </div>
    
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const canvas = document.getElementById('scratchCanvas');
            const ctx = canvas.getContext('2d');
            const resetBtn = document.getElementById('resetBtn');
            const newCardBtn = document.getElementById('newCardBtn');
            const recordsList = document.getElementById('recordsList');
            const prizeText = document.querySelector('.prize-text');
            const prizeSubtext = document.querySelector('.prize-subtext');
            
            // 奖品配置
            const prizes = [
                { name: "一等奖", probability: 5 },
                { name: "二等奖", probability: 10 },
                { name: "三等奖", probability: 15 },
                { name: "幸运奖", probability: 30 },
                { name: "谢谢参与", probability: 40 }
            ];
            
            let currentPrize = null;
            let isDrawing = false;
            let records = JSON.parse(localStorage.getItem('scratchRecords')) || [];
            
            // 初始化刮刮卡
            function initScratchCard() {
                // 设置canvas尺寸
                canvas.width = canvas.offsetWidth;
                canvas.height = canvas.offsetHeight;
                
                // 绘制涂层
                drawScratchLayer();
                
                // 重置奖品显示
                prizeText.textContent = "恭喜您获得";
                prizeSubtext.textContent = "刮开涂层查看奖品";
                
                // 随机生成新奖品
                if (!currentPrize) {
                    generateNewPrize();
                }
            }
            
            // 绘制刮刮卡涂层
            function drawScratchLayer() {
                ctx.fillStyle = '#4facfe';
                ctx.fillRect(0, 0, canvas.width, canvas.height);
                
                // 添加纹理效果
                ctx.fillStyle = 'rgba(255, 255, 255, 0.2)';
                for (let i = 0; i < 50; i++) {
                    const x = Math.random() * canvas.width;
                    const y = Math.random() * canvas.height;
                    const size = Math.random() * 20 + 10;
                    ctx.beginPath();
                    ctx.arc(x, y, size, 0, Math.PI * 2);
                    ctx.fill();
                }
                
                // 添加文字提示
                ctx.font = 'bold 24px "Microsoft YaHei"';
                ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
                ctx.textAlign = 'center';
                ctx.fillText('刮开此处', canvas.width / 2, canvas.height / 2);
            }
            
            // 生成新奖品
            function generateNewPrize() {
                const totalProbability = prizes.reduce((sum, prize) => sum + prize.probability, 0);
                let randomNum = Math.random() * totalProbability;
                
                for (const prize of prizes) {
                    if (randomNum < prize.probability) {
                        currentPrize = prize.name;
                        break;
                    }
                    randomNum -= prize.probability;
                }
            }
            
            // 添加抽奖记录
            function addRecord(prize) {
                const now = new Date();
                const timeString = now.toLocaleString('zh-CN', {
                    year: 'numeric',
                    month: '2-digit',
                    day: '2-digit',
                    hour: '2-digit',
                    minute: '2-digit',
                    second: '2-digit',
                    hour12: false
                });
                
                const record = {
                    prize: prize,
                    time: timeString
                };
                
                records.unshift(record);
                localStorage.setItem('scratchRecords', JSON.stringify(records));
                
                updateRecordsDisplay();
            }
            
            // 更新记录显示
            function updateRecordsDisplay() {
                recordsList.innerHTML = '';
                
                if (records.length === 0) {
                    recordsList.innerHTML = '<div class="empty-records">暂无抽奖记录</div>';
                    return;
                }
                
                records.forEach(record => {
                    const recordItem = document.createElement('div');
                    recordItem.className = 'record-item';
                    recordItem.innerHTML = `
                        <div class="record-prize">${record.prize}</div>
                        <div class="record-time">${record.time}</div>
                    `;
                    recordsList.appendChild(recordItem);
                });
            }
            
            // 检查刮开面积
            function checkScratchedArea() {
                const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
                const pixels = imageData.data;
                let transparentPixels = 0;
                let totalPixels = canvas.width * canvas.height;
                
                for (let i = 0; i < pixels.length; i += 4) {
                    if (pixels[i + 3] === 0) {
                        transparentPixels++;
                    }
                }
                
                const scratchedPercent = (transparentPixels / totalPixels) * 100;
                return scratchedPercent;
            }
            
            // 事件监听
            canvas.addEventListener('mousedown', function(e) {
                isDrawing = true;
                ctx.globalCompositeOperation = 'destination-out';
                drawAtPosition(e.clientX, e.clientY);
            });
            
            canvas.addEventListener('mousemove', function(e) {
                if (isDrawing) {
                    drawAtPosition(e.clientX, e.clientY);
                    
                    // 检查刮开面积
                    const scratchedPercent = checkScratchedArea();
                    if (scratchedPercent > 40) {
                        // 显示奖品
                        prizeText.textContent = currentPrize;
                        prizeSubtext.textContent = "恭喜中奖！";
                        prizeText.classList.add('pulse');
                        
                        // 添加记录
                        addRecord(currentPrize);
                        
                        // 禁用绘制
                        isDrawing = false;
                        
                        // 移除动画类
                        setTimeout(() => {
                            prizeText.classList.remove('pulse');
                        }, 500);
                    }
                }
            });
            
            canvas.addEventListener('mouseup', function() {
                isDrawing = false;
            });
            
            canvas.addEventListener('mouseleave', function() {
                isDrawing = false;
            });
            
            // 触摸事件支持
            canvas.addEventListener('touchstart', function(e) {
                e.preventDefault();
                isDrawing = true;
                ctx.globalCompositeOperation = 'destination-out';
                const touch = e.touches[0];
                drawAtPosition(touch.clientX, touch.clientY);
            });
            
            canvas.addEventListener('touchmove', function(e) {
                e.preventDefault();
                if (isDrawing) {
                    const touch = e.touches[0];
                    drawAtPosition(touch.clientX, touch.clientY);
                    
                    // 检查刮开面积
                    const scratchedPercent = checkScratchedArea();
                    if (scratchedPercent > 40) {
                        // 显示奖品
                        prizeText.textContent = currentPrize;
                        prizeSubtext.textContent = "恭喜中奖！";
                        prizeText.classList.add('pulse');
                        
                        // 添加记录
                        addRecord(currentPrize);
                        
                        // 禁用绘制
                        isDrawing = false;
                        
                        // 移除动画类
                        setTimeout(() => {
                            prizeText.classList.remove('pulse');
                        }, 500);
                    }
                }
            });
            
            canvas.addEventListener('touchend', function() {
                isDrawing = false;
            });
            
            // 绘制函数
            function drawAtPosition(x, y) {
                const rect = canvas.getBoundingClientRect();
                const posX = x - rect.left;
                const posY = y - rect.top;
                
                ctx.beginPath();
                ctx.arc(posX, posY, 25, 0, Math.PI * 2);
                ctx.fill();
            }
            
            // 重置刮刮卡
            resetBtn.addEventListener('click', function() {
                initScratchCard();
            });
            
            // 生成新刮刮卡
            newCardBtn.addEventListener('click', function() {
                generateNewPrize();
                initScratchCard();
            });
            
            // 初始化
            initScratchCard();
            updateRecordsDisplay();
        });
    </script>
</body>
</html>
